<?php
/*
 * Created on May 7, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
function extractURL($text) {
   	if(preg_match('@(https?://[^ ]*)@',$text,$matches)) {
   		$url = $matches[0];
	}
	else $url='';
   	return $url;
}
function createEmboodle($description) {
	$url=extractURL($description);
	
	$url_parts = parse_url($url);
	if(!isset($url_parts['host']))
		return;
 
    //$embedcode = 'failure';
    //$status='unknown_domain';
	$status='success';
    //Set up embed fetching
    $oembed_endpoint='http://oohembed.com/oohembed/?url='; 
	$request=$oembed_endpoint . urlencode($url);
	$encoded_response = file_get_contents($request);
	$response= json_decode($encoded_response,true);

	//Pull relevant info into my variables	
	$title=$response['title'];
	if($response['type']=='video') {
		$embedcode=$response['html'];
	} elseif($response['type']=='photo') {
		$embedcode='<a href="'.$url.'"><img src="'.$response['url'].'"/></a>';
	}

   	//Fetch description (oembed doesn't do this)
   	$query = "select * from html where url = \"".$url. "\" and xpath =\"//head\"";
	$request= "http://query.yahooapis.com/v1/public/yql?q=".urlencode($query);
    $yql_response=file_get_contents($request);
    $response_xml = simplexml_load_string($yql_response);
    $results= $response_xml->xpath('//results');
    $xml = $results[0];    
    $description_result=$xml->xpath("//meta[@name='description']/attribute::content");
    $description=(string)$description_result[0];
   		
   	//create array 
   	$array = array( 'embedcode' => $embedcode,
					'title'=>$title,
   	 				'url' => $url,
					'status' => $status, 
					'description' =>$description,
					);
	return $array;
   	
}

?>

